// index.js
// 获取应用实例
var that,
  app = getApp(),
  api= new (require('../../utils/api.js')),
  headimg = api.getimgurl(), 
  util=new (require('../../utils/util')); 


Page({
  data: {
    list:[],
    showModel:!1,
    showbModel:!1,
    headimg,
    operate:'',
    phone:'',
    pics:[
      '111.jpg',
      '222.jpg',
      '333.jpg',
      '444.jpg',
     
    ],
    issuper:0,
    userInfo: {},
    caninfo:!1,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  //27.11382   lng 114.99376  
  golocation() {
wx.openLocation({
  latitude: 27.100528899,
  longitude: 115.01880945,
})



// wx.chooseLocation({
//   latitude: 0,
//   success:res=>{
//     console.log(res,'111'); 
//   }
// })


    // wx.navigateTo({
    //   url: '../logs/logs'
    // })
  },

  delsuper(e){
    wx.showModal({
      title:'提示',
      content: '确定要移除吗',
      success:res=>{
        if(res.confirm){
          api.tosonsumer(e.currentTarget.dataset.id,res=>{
            if(res.data.issuccess==1){
              api.supergetlist(res=>{
                if(res.data.issuccess==1){
                  that.setData({list:res.data.data})
                }else{
                  util.nocancelmodal(res.data.msg)
                }
              })
            }else{
              util.nocancelmodal(res.data.msg)
            }
          })
        }
      }
    })
  },

  todetail(e){
    console.log(e,'itemitem');
    let{item} = e.currentTarget.dataset,{userid,avatar,name,phone,beers,points} = item,data={avatar,name,phone,beers,points};
    that.setData({seledata:data})
    wx.navigateTo({
      url: '../detail/detail?userid='+userid,
    })
  },

  customer_pay(){
    that.setData({showModel:!0})
  },

  onLoad() {
  that=this;
  // if(wx.getUserProfile){
  //   that.setData({caninfo:!0})
  // }

  if (app.logininfo) {
    that.getdata()
   } else {
     util.setwatcher(app, this.afterlogin)
   }

  },
  onShow(){
    if(that.data.needfresh){
      that.getdata()
      that.setData({needfresh:!1})
    }
  },
  afterlogin: {
    logininfo: res => {
      that.getdata()
    } 
  },
  search(e){
    that.setData({keyword:e.detail.value})
  },
  searchtap(){
    that.setData({list:[]})
    let{keyword} = this.data;
    if(keyword.length>0&&keyword.length<12){
      wx.showLoading({
        title: '加载中',
      })
      api.supergetlistbyword(keyword,res=>{
        if(res.data.issuccess==1){
          that.setData({list:res.data.data})
        }else{
          util.nocancelmodal(res.data.msg)
        }
      })

    }
  },

  getdata(){
    if(app.logininfo.user.type=='CONSUMER'){
      console.log(app.logininfo,'app.logininfo');
      if(app.logininfo.user.phone){
        that.setData({islogin:!0})
        api.getaccountlist(res=>{
          if(res.data.issuccess==1){
            let {list,beers} =res.data,newlist = [];
            list.forEach(x=>{
              x['datestr'] = util.formatTime(new Date(Number(x.updateDate)*1000))
              newlist.push(x)
            }) 
            that.setData({list:res.data.list,storage:res.data.storage||0,beer:beers})
          }else{
            util.nocancelmodal(res.data.msg)
          }
  
        })
      }
    }
    if(app.logininfo.user.type=='SUPERADMIN'){
      api.supergetlist(res=>{
        if(res.data.issuccess==1){
          that.setData({list:res.data.data})
        }else{
          util.nocancelmodal(res.data.msg)
        }
      })
      that.setData({issuper:1})
    }
    
    // let{zhichi,zhuban}=app.logininfo
    // zhichi=zhichi.split(','),zhuban=zhuban.split(',');
    // that.setData({zhichi,zhuban})

    
   // this.loginin('122','333333')
  
  },
  tosuper(e){
    wx.showModal({
      title:'提示',
      content: '确定成为超管吗',
      success:res=>{
        if(res.confirm){
          api.tobesuper(e.currentTarget.dataset.id,res=>{
            if(res.data.issuccess==1){
              api.supergetlist(res=>{
                if(res.data.issuccess==1){
                  that.setData({list:res.data.data})
                }else{
                  util.nocancelmodal(res.data.msg)
                }
              })
            }else{
              util.nocancelmodal(res.data.msg)
            }
          })
        }
      }
    })
  },
  setaccount(e){
    let {r,id} = e.currentTarget.dataset;
    that.setData({operate:r,showModel:!0,setuid:id})
  },

  setbeer(e){
    let {r,id} = e.currentTarget.dataset;
    console.log('beer',{r,id});
    that.setData({operate:r,showbModel:!0,setuid:id})
  },

  setmoney(e){
    that.setData({inputmoney:e.detail.value})
  },
  inputsetbeer(e){
    that.setData({inputbeer:e.detail.value})
  },

  confirmb(e){
    let{operate,inputbeer,setuid} = this.data,type=Number(operate)==0?'DBEER':'ABEER';
    let data = {type,id:setuid,point:Number(inputbeer)};
    console.log(data,'confirm--data');
    api.supersetbeer(data,res=>{
      if(res.data.issuccess==1){
        util.nocancelmodal('成功！','提示',res=>{
          that.setData({showbModel:!1,setbeerval:''})
          api.supergetlist(res=>{
            if(res.data.issuccess==1){
              that.setData({list:res.data.data})
            }else{
              util.nocancelmodal(res.data.msg)
            }
          })
        })



      }else{
        util.nocancelmodal(res.data.msg)
      }
    })
    // if(Number(operate)==0){ 
    //   util.nocancelmodal('消费'+inputmoney+"元")
      
    // }else{
    //   util.nocancelmodal('充值'+inputmoney+"元")
    // }
  },

  


  confirm(e){
    let{operate,inputmoney,storage} = this.data,type=Number(operate)==0?'XF':'CZ',point = Math.round(Number(inputmoney)*100);
    if(point>storage){
      util.nocancelmodal('输入金额有误！')
    }else{
      wx.showModal({
        title: '提示',
        content: '确定要支付'+inputmoney+'元吗',
        complete: (res1) => {
          if (res1.confirm) {
            let data = {point}; 
            console.log(data,'confirm--data');
            api.custompay(data,res=>{
              if(res.data.issuccess==1){
                util.nocancelmodal('成功！','提示',res=>{
                  that.setData({showModel:!1,setmoneyval:''})
                  api.getaccountlist(res=>{
                    if(res.data.issuccess==1){
                      let {list} =res.data,newlist = [];
                      list.forEach(x=>{
                        x['datestr'] = util.formatTime(new Date(Number(x.updateDate)*1000))
                        newlist.push(x)
                      }) 
                      that.setData({list:res.data.list,storage:res.data.storage||0})
                    }else{
                      util.nocancelmodal(res.data.msg)
                    }
                  })
                })
              }else{
                util.nocancelmodal(res.data.msg)
              }
            })
          }
        }
      })


    
    }
   
    // if(Number(operate)==0){ 
    //   util.nocancelmodal('消费'+inputmoney+"元")
      
    // }else{
    //   util.nocancelmodal('充值'+inputmoney+"元")
    // }
  },
  input(e){
   
    that.setData({phone:e.detail.value})
  },
  deluser(e){
    wx.showModal({
      title:'提示',
      content: '确定要删除吗',
      success:res=>{
        if(res.confirm){
          api.superdel(e.currentTarget.dataset.id,res=>{
            if(res.data.issuccess==1){
              api.supergetlist(res=>{
                if(res.data.issuccess==1){
                  that.setData({list:res.data.data})
                }else{
                  util.nocancelmodal(res.data.msg)
                }
              })
            }else{
              util.nocancelmodal(res.data.msg)
            }
          })
        }
      }
    })
  },
  onShareAppMessage(){

  },
  
  onPullDownRefresh(){
    console.log('onPullDownRefresh');
    that.setData({list:[]},()=>{
      that.getdata()
      wx.stopPullDownRefresh()
    })
  },
  cancel(e){
    that.setData({showModel:!1})
  },
  cancelb(e){
    that.setData({showbModel:!1})
  },
  getinfo(e){
    let {phone} = this.data;
    if(phone.length!=11){
      util.nocancelmodal('输入有误')
      return;
    }
    api.updateinfo({phone},res1=>{
      if(res1.data.issuccess==1){
       let Info = wx.getStorageSync('userInfo');
       Info.avatarUrl='';
       Info.nickname='';
       wx.setStorageSync('userInfo', Info)
       that.setData({islogin:!0,storage:0})
      }else{
        console.log(res1.data.msg,'res.data.msg');
        util.nocancelmodal(res1.data.msg)
      }
    })




  //  wx.getUserProfile({
  //    desc: '用于完善会员资料',
  //    success:res=>{
  //      let {userInfo} =res,{nickName,avatarUrl} = userInfo;
      
  //    },
  //  })
  },

  preimg(e){
    let{src} = e.currentTarget.dataset;
    wx.previewImage({
      urls: [src],
     
    })
  },

  call(){
  wx.makePhoneCall({
    phoneNumber: '18797967358',
  })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
